package com.weixin.project.Config;

import com.weixin.project.Entity.User;
import com.weixin.project.Service.CommonService;
import com.weixin.project.Service.ForumService;
import com.weixin.project.Service.RecommendService;
import com.weixin.project.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
public class ScheduleConfig {
    @Autowired
    CommonService commonService;
    @Autowired
    UserService userService;
    @Autowired
    RecommendService recommendService;
    @Autowired
    ForumService forumService;
    @Scheduled(fixedRate = 1000 * 2 * 3600)
    public void transLikeFromRedis2DB(){
        System.out.println("loading user like data from redis to DB...");
        commonService.transLikeFromRedis2DB();
    }

    @Scheduled(fixedRate = 1000 * 2 * 3600)
    public void transReadCountOfForumQuestionFromRedis2DB(){
        System.out.println("loading read count of forum question from redis to DB...");
        forumService.transReadCountFromRedis2DB();
    }

    @Scheduled(fixedRate = 1000 * 1 * 3600)
    public void incLR(){
        System.out.println("incTraining LR model...");
        recommendService.incLR();
    }

//    @Scheduled(fixedRate = 1000 * 1 * 3600)
//    public void updateLR(){
//        System.out.println("开始更新用户的LR参数...");
//        Set<User> onlineUsers = userService.findAllOnlineUsers();
//        for (User user: onlineUsers){
//            recommendService.updateLRWeight(user.getId());
//        }
//    }

    @Scheduled(cron = "0 0 0 * * ? ")
    public void clearTodayLoginSetInRedis(){
        userService.clearTodayLoginSetInRedis();
    }
}
